package com.ruoyi.cms.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baidu.ueditor.ActionEnter;
import com.baidu.ueditor.upload.IStorageManager;
import com.baidu.ueditor.upload.StorageManager;
import com.ruoyi.cms.domain.Attachment;
import com.ruoyi.cms.domain.Pv;
import com.ruoyi.cms.service.CmsService;
import com.ruoyi.cms.service.IAttachmentService;
import com.ruoyi.cms.service.IPvService;
import com.ruoyi.cms.service.PVQueueService;
import com.ruoyi.cms.util.OSSStorageManager;
import com.ruoyi.cms.util.OssActionEnter;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.config.Global;
import com.ruoyi.common.config.ServerConfig;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.file.MimeTypeUtils;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.oss.cloud.CloudStorageService;
import com.ruoyi.oss.cloud.OSSFactory;
import com.ruoyi.oss.domain.SysOss;
import com.ruoyi.oss.service.ISysOssService;
import com.ruoyi.system.domain.SysUser;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * cms模块的通用操作
 */
@Controller
public class CmsController extends BaseController {

    @Value("${cms.file.upload.path}")
    private String dirPath;

    @Autowired
    private ServerConfig serverConfig;
    @Autowired
    private IAttachmentService attachmentService;

    @Autowired
    private IPvService pvService;
    @Autowired
    CmsService cms;

    @Autowired
    ISysOssService ossService;
    /**
     * 上传附件请求
     */
    @PostMapping("/cms/uploadAttach")
    @ResponseBody
    public AjaxResult uploadAttach(@RequestParam("zid") String zid,String sort, MultipartFile file) throws Exception
    {
        try
        {
            if(StringUtils.isEmpty(zid)){
                return AjaxResult.error("参数[zid]不能为空!");
            }
            Attachment attachment=new Attachment();
            if(StringUtils.isEmpty(sort)){
                sort="1";
            }
            int sortNum=1;
            try{
                sortNum=Integer.valueOf(sort);
            }catch (Exception ex){}
            attachment.setSort(sortNum);
            String suffix=FileUploadUtils.getExtension(file);
            String fileType= MimeTypeUtils.MATERIAL_TYPE_OTHER;//文件类型-其它
            if(FileUploadUtils.isImage(suffix)){
                fileType=MimeTypeUtils.MATERIAL_TYPE_IMG;
            }else if(FileUploadUtils.isText(suffix)){
                fileType=MimeTypeUtils.MATERIAL_TYPE_TEXT;
            }else if(FileUploadUtils.isAudio(suffix)){
                fileType=MimeTypeUtils.MATERIAL_TYPE_AUDIO;
            }else if(FileUploadUtils.isVideo(suffix)){
                fileType=MimeTypeUtils.MATERIAL_TYPE_VIDEO;
            }else if(FileUploadUtils.isZip(suffix)){
                fileType=MimeTypeUtils.MATERIAL_TYPE_ZIP;
            }else{
                fileType= MimeTypeUtils.MATERIAL_TYPE_OTHER;
            }


//            CloudStorageService storage = OSSFactory.build();
//            String url = storage.uploadSuffix(file.getBytes(), suffix);
//            SysOss sysOss = new SysOss();
//            ossService.save(sysOss);

            // 上传并返回新文件名称
//            String path = FileUploadUtils.upload(Global.getAttachPath(), file);
//            String url = serverConfig.getUrl() + path;

            CloudStorageService storage = OSSFactory.build();
            String path = FileUploadUtils.getPath(Global.getMaterialPath(), file);
            String url = storage.uploadSuffix(file.getBytes(), path);
            logger.info("上传 附件 url：" + url);


            attachment.setFileType(fileType);
            attachment.setFileUrl(url);
            attachment.setFilePath(path);
            attachment.setSize(file.getSize());
            attachment.setFileName(file.getOriginalFilename());
            attachment.setZid(zid);

            attachmentService.insertAttachment(attachment);
            return AjaxResult.success(attachment);
        }
        catch (Exception e)
        {
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 根据组ID查询附件列表
     */
    @PostMapping("/cms/selectAttachmentsByZid")
    @ResponseBody
    public Object selectAttachmentsByZid(@RequestParam("zid")String  zid)
    {
        List<Attachment> list = attachmentService.selectAttachmentsByZid(zid);
        return AjaxResult.success(list);
    }

    /**
     * 删除附件
     */
    @Log(title = "附件", businessType = BusinessType.DELETE)
    @PostMapping( "/cms/deleteAttachments")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        SysUser user= ShiroUtils.getSysUser();
        boolean checkOkFlag=true;
        if(!user.isAdmin()){
            //非管理员用户调用该接口删除只能删除自己的附件
            String[] arr= Convert.toStrArray(ids);
            for(String id:arr){
                if(StringUtils.isNotEmpty(id)){
                    Attachment attachment=attachmentService.selectAttachmentById(id);
                    if(attachment!=null){
                        if(!attachment.getUserId().equals(user.getUserId().toString())){
                            checkOkFlag=false;
                            break;
                        }
                    }
                }
            }
        }
        if(!checkOkFlag){
            return AjaxResult.error("您只能删除自己上传的附件!");
        }
        return toAjax(attachmentService.deleteAttachmentByIds(ids));
    }


    /**
     * 上传素材
     */
    @PostMapping("/cms/uploadMaterial")
    @ResponseBody
    public AjaxResult uploadMaterial(MultipartFile file) throws Exception
    {
        try
        {

            CloudStorageService storage = OSSFactory.build();
            String path = FileUploadUtils.getPath(Global.getMaterialPath(), file);

            if (path.startsWith("/")){
                path = path.replaceFirst("/","");
            }
            String url = storage.upload(file.getBytes(), path);
            logger.info("上传 素材 url：" + url);

//            // 上传并返回新文件名称
//            String path = FileUploadUtils.upload(Global.getMaterialPath(), file);
//            String url = serverConfig.getUrl() + path;



            Map<String,Object> data=new HashMap();
            int width=FileUploadUtils.getImgWidth(file);
            int height=FileUploadUtils.getImgHeight(file);
            data.put("path",path);
            data.put("url",url);
            data.put("width",width);
            data.put("suffix",FileUploadUtils.getExtension(file));
            data.put("height",height);
            data.put("size",file.getSize());
            data.put("name",file.getOriginalFilename());
            return AjaxResult.success(data);
        }
        catch (Exception e)
        {
            return AjaxResult.error(e.getMessage());
        }
    }

    @Autowired
    PVQueueService pvQueueService;

    @PostMapping( "/page/view")
    @ResponseBody
    public AjaxResult pv(Pv pv, HttpServletRequest request)
    {
        pvQueueService.pushPvQueue(request,pv);
        return success();
    }

    @PostMapping( "/cms/clearSiteInfoCache")
    @ResponseBody
    public AjaxResult clearSiteInfoCache(Pv pv, HttpServletRequest request)
    {
        cms.clearCache();
        return success();
    }

    @GetMapping("cms/ueditor/config")
    public void ueditorConfig(HttpServletRequest request, HttpServletResponse response)
    {
//        StorageManager storageManager = new StorageManager();
//        ActionEnter actionEnter = new ActionEnter(storageManager,request,dirPath,"/config.json");
//        String resultString = actionEnter.exec();


//        response.setContentType("application/json");
//        String rootPath = request.getSession().getServletContext().getRealPath("/");
//        try {
//            String exec = new ActionEnter(request, rootPath).exec();
//            PrintWriter writer = response.getWriter();
//            writer.write(exec);
//            writer.flush();
//            writer.close();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }

        response.setContentType("application/json");
        String rootPath = request.getSession().getServletContext().getRealPath("/");
        try {
            OSSStorageManager storageManager = new OSSStorageManager();
            OssActionEnter actionEnter = new OssActionEnter(storageManager,request,dirPath,"/config.json");
            String resultString = actionEnter.exec();
            PrintWriter writer = response.getWriter();
            writer.write(resultString);
            writer.flush();
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }


//        File jsonFile = null;
//        try {
//            jsonFile = ResourceUtils.getFile("classpath:config.json");
//        } catch (FileNotFoundException e) {
//            e.printStackTrace();
//        }
//        // 解析文件为指定编码的字符串
//        // 方法实现：先将文件转inPutStream，再调用下面的IOUtils.toString()方法；
//        String json = null;
//        try {
//            json = FileUtils.readFileToString(jsonFile,"UTF-8");
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        JSONObject jsonArray = JSON.parseObject(json);
//        return jsonArray;
    }

    @PostMapping("cms/ueditor/config")
    public void ueditorConfigPost(HttpServletRequest request, HttpServletResponse response)
    {

        response.setContentType("application/json");
        String rootPath = request.getSession().getServletContext().getRealPath("/");
        try {
            OSSStorageManager storageManager = new OSSStorageManager();
            OssActionEnter actionEnter = new OssActionEnter(storageManager,request,dirPath,"/config.json");
            String resultString = actionEnter.exec();
            PrintWriter writer = response.getWriter();
            writer.write(resultString);
            writer.flush();
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
